<?php
/**
 * Created by swagger_zxd.
 * User: zxd
 * Date: 2018/1/19
 * Time: 10:29
 */

namespace app\player\controller;


use app\index\controller\Base;
use app\player\model\Player;
use think\Db;

class Playerinfo extends Base
{
    /**
     * 玩家搜索列表
     * @return mixed|\think\response\Json
     */
    public function searchPlayerList(){
        if ($this->request->method()=="POST"){
            $data=$this->request->param();

            $page=$data['page']?$data['page']:1;
            $pageSize=$data['pageSize'];
            
            $uType =          $data["uType"];//搜索项
            $userid =         $data['userid']?$data['userid']:nullParamFormat($data["userid"]);//搜索的值
            $begin =          $data['begin']?"'".$data['begin']."'":nullParamFormat($data["begin"]);//注册时间
            $end =            $data['end']?"'".$data['end']."'":nullParamFormat($data["end"]);//注册时间
            $beginLog =       $data['beginLog']?"'".$data['beginLog']."'":nullParamFormat($data["beginLog"]);//最后登录时间
            $endLog =         $data['endLog']?"'".$data['endLog']."'":nullParamFormat($data["endLog"]);//最后登录时间
            $minScore =       $data['minScore']?$data['minScore']:nullParamFormat($data["minScore"]);//用户金币
            $maxScore =       $data['maxScore']?$data['maxScore']:nullParamFormat($data["maxScore"]);//用户金币

            $minPrize =       $data['minPrize']?$data['minPrize']:nullParamFormat($data["minPrize"]);//奖券
            $maxPrize =       $data['maxPrize']?$data['maxPrize']:nullParamFormat($data["maxPrize"]);//奖券
            $minZsy =         $data['minZsy']?$data['minZsy']:nullParamFormat($data["minZsy"]);//总输赢
            $maxZsy =         $data['maxZsy']? $data['maxZsy']: nullParamFormat($data["maxZsy"]);//总输赢
            $minDrsy =        $data['minDrsy']?$data['minDrsy'] : nullParamFormat($data["minDrsy"]);//当日输赢
            $maxDrsy =        $data['maxDrsy']?$data['maxDrsy'] : nullParamFormat($data["maxDrsy"]);//当日输赢
            $minZengSongCha = $data['minZengSongCha']? $data['minZengSongCha']: nullParamFormat($data["minZengSongCha"]);//金币赠送差(接收-赠送)
            $maxZengSongCha = $data['maxZengSongCha']? $data['maxZengSongCha']: nullParamFormat($data["maxZengSongCha"]);//金币赠送差(接收-赠送)
            $phone =		  $data['phone']?$data['phone'] : nullParamFormat($data["phone"]);//密保手机
            $loginIp =   	  $data['loginIp']?$data['loginIp'] : nullParamFormat($data["loginIp"]);//登录IP
            $regIp =	      $data['regIp']?$data['regIp'] : nullParamFormat($data["regIp"]);//注册IP
            $userState =      $data['userState']?$data['userState'] : nullParamFormat($data["userState"]);//账号状态
            $userType =       $data['usertype']?$data['usertype'] : nullParamFormat($data["usertype"]);//用户类型
            $platform =       $data['platform']?$data['platform'] : nullParamFormat($data["platform"]);//游戏平台
            $state =          $data['state']?$data['state'] : paramFormat($data["state"]);//控制状态
            $zbType =         $data['zbType']?$data['zbType'] : paramFormat($data["zbType"]);//作弊类型
            $gamePlatform =   $data['gamePlatform']? $data['gamePlatform']: paramFormat($data["gamePlatform"]);//游戏平台
            $sortKey =        $data["sortKey"];//排序的列
            $sortVal =        $data["sortVal"];//排序方式
            $order = "";
            if($sortKey == "" && $sortVal == ""){
                $order = "null";
            }else{
                $order = "' order by ".$sortKey." ".$sortVal."'";
            }
//            dump($begin);
            $PlayerModel=new Player();
            return $PlayerModel->searchPlayerListData(
                $page,$pageSize,$uType,$userid,$begin,$end,$beginLog,
                $endLog,$minScore,$maxScore,$minPrize,$maxPrize,$minZsy,
                $maxZsy,$minDrsy,$maxDrsy,$minZengSongCha,$maxZengSongCha,
                $phone,$loginIp,$regIp,$userState,$userType,$platform,$state,
                $zbType,$gamePlatform,$order
            );

        }else{
            return $this->fetch("searchPlayerList");
        }
    }

    /**
     * 用户点控记录
     * @return mixed|\think\response\Json
     */
    public function playerDianKongList()
    {
        if ($this->request->method()=="POST"){
            $data=$this->request->param();

            $page=$data['page']?$data['page']:1;
            $begin = $data["begin"]?"'".$data["begin"]."'":'null';
            $end = $data["end"]?"'".$data['end']."'":'null';
            $gm = $data["gm"]?"'".$data['gm']."'":'null';
            $gameId = $data["gameId"]?"0,'".$data['gameId']."'":'null,null';
            $winLose = $data["shuYing"]==""?'null':$data['shuYing'];

            try{

                $pdo=odbcLink();

                $sqlstr="exec Web_Gm_UpdateRateList ".$gm.",".$gameId.",".$begin.",".$end.",".$winLose.",".$page."";

                $sth=$pdo->query($sqlstr);
                $i=1;

                $data=['records'=>''];

                do{
                    $rowset = $sth->fetchAll(\PDO::FETCH_BOTH);
                    if ($rowset) { //
                        if(1<$i && $i<=4){
                            switch($i){
                                case 2:
                                    $count=$rowset[0][0];
                                    $page=$page;
                                    break;
                                case 3:
                                    $winSCORE=$rowset[0][0];
                                    break;
                                case 4:
                                    $loseSCORE=$rowset[0][0];
                            }
                        }elseif($i>4){
                           $data['records']=$rowset;
                        }
                    }
                    $i++;
                }while($sth->nextRowset());

            }catch(\Exception $e){
               echo $e->getMessage();
            }

            $records=washData($data['records']);


            $outp=['pageinfo'=>['count'=>$count,'page'=>$page],'winScore'=>$winSCORE,'loseScore'=>$loseSCORE,'records'=>$records];

            return json($outp);

        }else{
            return $this->fetch("playerDianKongList");
        }
    }

    /**
     * 玩家详情
     * @return \think\response\Json
     */
    public function playerDetailsInfo()
    {
        $input=$this->request->param();
        $gameid=$input['gameId'];
        if ($this->request->method()=="POST"){
            try{
                $pdo=odbcLink();
                $sqlstr="exec bg_SelectUserDetail2 '".$gameid."'";
                $sth=$pdo->query($sqlstr);

                $i=1;
                do{
                    $rowset = $sth->fetchAll(\PDO::FETCH_ASSOC);
//                        dump($i);
//                        dump($rowset);
                        if($rowset){
                            switch($i){
                                case 1:
                                    foreach($rowset as $k=>$value){
                                        $rowset[$k]['NickName']=iconv("gbk", "utf-8",$rowset[$k]['NickName']);
                                        $rowset[$k]['Accounts']=iconv("gbk", "utf-8",$rowset[$k]['Accounts']);
                                        $rowset[$k]['PackName']=iconv("gbk", "utf-8",$rowset[$k]['PackName']);
                                        $rowset[$k]['PlatformName']=iconv("gbk", "utf-8",$rowset[$k]['PlatformName']);
                                        $rowset[$k]['zx']=iconv("gbk", "utf-8",$rowset[$k]['zx']);
                                        $rowset[$k]['LastResDomain']=iconv("gbk", "utf-8",$rowset[$k]['LastResDomain']);
                                        $rowset[$k]['RoomName']=iconv("gbk", "utf-8",$rowset[$k]['RoomName']);
                                        $rowset[$k]['LastLoginDomain']=iconv("gbk","utf-8",$rowset[$k]['LastLoginDomain']);
//                                        dump($rowset[$k]['NickName']);
                                    }

                                    $data[]=$rowset[0];
                                    break;
                                case 2:
                                    $data['hitCount']= $rowset[0]['HitCounts'];
                                    break;
                                case 3:
                                    $data['hitMoney']=$rowset[0]['HitRMB'];
                                    break;
                                case 4:
                                    $data['dhMoney']=$rowset[0]['ExchangeRMB'];
                                    $data['dhCount']=$rowset[0]['ExchangeCounts'];
                                    break;
                                case 5:
                                    $data['limitScoreRiseValue']=$rowset[0]['LimitScoreRiseValue'];
                                    $data['maxWin']=numberFormat($rowset[0]['WinMax']);
                                    $data['minWin']=numberFormat($rowset[0]['WinMin']);
                                    $data['maxLose']=numberFormat($rowset[0]['LoseMax']);
                                    $data['minLose']=numberFormat($rowset[0]['LoseMin']);

                            }
                        }

                    $i++;
                }while($sth->nextRowset());
            }catch(\Exception $e){
                echo $e->getMessage();
            }
//            var_dump($data);
//            exit();
            return json($data);
        }else{
            //$this->buildHtml($gameid,APP_PATH.'application/player/view',APP_PATH.'application/player/tpl/playerDetailsInfo.html');
            return $this->fetch("playerDetailsInfo",['gameid'=>$gameid]);
        }
    }


    /**
     * 玩家登陆详情
     * @return mixed|\think\response\Json
     */
    public function playerloginloglist(){
        if ($this->request->method()=="POST") {
            $data=$this->request->param();
            $page=$data['page']?$data['page']:1;
            $pageSize=$data['pageSize'];
            $begin = $data["begin"]?"'".$data["begin"]."'":'null';
            $end = $data["end"]?"'".$data['end']."'":'null';
            $gameId = $data["gameId"]?"0,'".$data['gameId']."'":'null,null';
            $jqm = $data["jqm"]==""?'null':"'".$data['jqm']."'";

            try{
                $pdo=odbcLink();

                $sqlstr="exec GetUserLoginList ".$begin.",".$end.", ".$gameId.", null, ".$jqm.", ".$page."";

                $sth=$pdo->query($sqlstr);

                $i=1;
                $records='';
                $countInfo='';
                do{
                    $rowset = $sth->fetchAll(\PDO::FETCH_ASSOC);

                    if ($rowset) {
                            switch($i){
                                case 3:
                                    $count=$rowset[0]['c'];
                                    $page=$page;
                                    $pageInfo=['count'=>$count,'page'=>$page];
                                    break;
                                case 4:

                                    if(!empty($rowset)){
                                        foreach ($rowset as $k=>$value) {
                                            $records[$k]['id']=$rowset[$k]['ID'];
                                            $records[$k]['gameId']=$rowset[$k]['GameID'];
                                            $records[$k]['nickName']=iconv("gbk","utf-8",$rowset[$k]['NickName']);
                                            $records[$k]['roomName']=iconv("gbk","utf-8",$rowset[$k]['RoomName']);
                                            $records[$k]['score']=$rowset[$k]['score'];
                                            $records[$k]['loginTime']=$rowset[$k]['EnterTime'];
                                            $records[$k]['loginIp']=$rowset[$k]['ClientIP'];
                                            $records[$k]['jiqima']=$rowset[$k]['MachineSerial']?$rowset[$k]['MachineSerial']:'';
                                        }
                                    }else{
                                        $records='';
                                    }
                                    break;
                                case 5:
                                    $c=$rowset[0]['c'];
                                    $ip=$rowset[0]['ClientIP'];
                                    $countInfo =['c'=>$c,'ip'=>$ip];
                                    break;

                            }
                    }
                    $i++;
                }while($sth->nextRowset());

            }catch(\Exception $e){
                echo $e->getMessage();
            }

            $outp=['pageinfo'=>$pageInfo,'records'=>$records,'countInfo'=>$countInfo];

            return json($outp);

        } else {
            $input=$this->request->param();
            $gameid=$input['gameid'];
            return $this->fetch('',['gameid'=>$gameid]);
        }
    }

    /**
     * 金币变化列表
     * @return mixed
     */
    public function playerGoldChangeList(){
        if ($this->request->method()=="POST"){

        } else {
            $input=$this->request->param();
            $gameid=$input['gameid'];
            return $this->fetch('',['gameid'=>$gameid]);
        }
    }

    /**
     * 批量锁定
     */
    public function batchLockOrUnlockUser(){
        if($this->request->method()=="POST"){
            $data=$this->request->param();
            $gameIdStr=$data['gameIdStr'];
            $type=$data['type'];
            $remark=$data['remark'];
            $PlayerModel=new Player();
            return $PlayerModel->batchLockOrUnlockUser($gameIdStr,$type,$remark);
        }
    }

    public function isLock(){
        if($this->request->method()=="POST"){
            $data=$this->request->param();
            $gameId=$data['gameId'];
            $PlayModel=new Player();
            return $PlayModel->isLock($gameId);
        }
    }

}